package com.fsnbigdata.repository;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.SpecimenVideo;
import com.fsnbigdata.domain.QSpecimenVideo;
import com.fsnbigdata.domain.Specimen;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * = SpecimenVideoRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = SpecimenVideoRepositoryCustom.class)
@Transactional(readOnly = true)
public class SpecimenVideoRepositoryImpl extends QueryDslRepositorySupportExt<SpecimenVideo> implements SpecimenVideoRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    SpecimenVideoRepositoryImpl() {
        super(SpecimenVideo.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String SPECIMEN = "specimen";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String URL = "url";

    /**
     * TODO Auto-generated method documentation
     *
     * @param specimen
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<SpecimenVideo> findBySpecimen(Specimen specimen, GlobalSearch globalSearch, Pageable pageable) {
        QSpecimenVideo specimenVideo = QSpecimenVideo.specimenVideo;
        JPQLQuery<SpecimenVideo> query = from(specimenVideo);
        Assert.notNull(specimen, "specimen is required");
        query.where(specimenVideo.specimen.eq(specimen));
        Path<?>[] paths = new Path<?>[] { specimenVideo.specimen, specimenVideo.url };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(SPECIMEN, specimenVideo.specimen).map(URL, specimenVideo.url);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, specimenVideo);
    }
}
